package com.soboot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soboot.base.converter.BeanMapping;
import com.soboot.base.enums.DeleteStatus;
import com.soboot.base.util.StringUtil;
import com.soboot.system.entity.SysDict;
import com.soboot.system.entity.SysRole;
import com.soboot.system.mapper.SysRoleMapper;
import com.soboot.system.model.QueryRoleListReqModel;
import com.soboot.system.service.ISysRoleService;
import com.soboot.system.vo.DictVO;
import com.soboot.system.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author:
 * @createTime: 2022年09月26日 21:14:37
 * @version:
 * @Description:
 * @Copyright:
 */
@Service
public class RoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public IPage<SysRole> page(QueryRoleListReqModel reqModel) {

        QueryWrapper<SysRole> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted", DeleteStatus.NOT_DELETE.getValue());
        if(StringUtil.isNotBlank(reqModel.getEndTime())){
            wrapper.le("create_time",reqModel.getEndTime());
        }
        if(StringUtil.isNotBlank(reqModel.getStartTime())){
            wrapper.ge("create_time",reqModel.getStartTime());
        }
        wrapper.like(StringUtil.isNotBlank(reqModel.getRoleName()), "role_name", reqModel.getRoleName());
        IPage<SysRole> page = sysRoleMapper.selectPage(new Page<>(reqModel.getPageNum(), reqModel.getPageSize()), wrapper);
        return page;
    }

    @Override
    public RoleVO detail(Integer id) {

        SysRole sysRole = sysRoleMapper.selectById(id);
        return BeanMapping.map(sysRole, RoleVO.class);
    }

    @Override
    public void delete(Integer id) {

        UpdateWrapper<SysRole> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("deleted", DeleteStatus.DELETE.getValue());
        sysRoleMapper.update(null,wrapper);
    }
}
